﻿using MaterialDesignThemes.Wpf;
using NPOI.POIFS.FileSystem;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using UIWindow.Commands;
using UIWindow.Domains;
using UIWindow.Entitys;
using UIWindow.Enums;
using UIWindow.GlobalParameters;
using UIWindow.Helper;
using UIWindow.Services;
using UIWindow.Views.Dialogs;
using XCode;

namespace UIWindow.ViewModels
{
    public class DataQueryViewModel
    {
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime StartTime { get; set; }
        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime EndTime { get; set; }
        /// <summary>
        /// 批次号
        /// </summary>
        public string ProductModel { get; set; }

        /// <summary>
        /// 条码
        /// </summary>
        public string TaryNumber { get; set; }

        /// <summary>
        /// 员工ID
        /// </summary>
        public string EmployeeID { get; set; }
        /// <summary>
        /// 生产数据
        /// </summary>
        public ObservableCollection<ProductionData> WeightList { get; set; }

        public DataQueryService dataQueryService;
        private ParameterGlobal _parameter;
        public DataQueryViewModel(ParameterGlobal parameter)
        {
            _parameter = parameter;
            StartTime = DateTime.Now.Date;
            EndTime = DateTime.Now.Date;
            dataQueryService = new DataQueryService(parameter);
            WeightList = new ObservableCollection<ProductionData>();
        }

        /// <summary>
        /// 数据查询
        /// </summary>
        public DelegateCommand<Button> QueryData => new DelegateCommand<Button>(async sender =>
        {
            WeightList.Clear();
            if (StartTime.Year != EndTime.Year)
            {
                "请选择相同年份".MessageDialogs(Enums.MessageLevelType.Warning);
                return;
            }
            if (GetQuarterly(StartTime.Month) != GetQuarterly(EndTime.Month))
            {
                $"请选择季度内查询\r\n1月至3月为一个季度\r\n4月至6月为一个季度\r\n7月至9月为一个季度\r\n10月至12月为一个季度\r\n".MessageDialogs(Enums.MessageLevelType.Warning);
                return;
            }
            _ = DialogHost.Show(new LoadingDialogs(), "RootDialog").ConfigureAwait(false);
            IList<ProductionData> list = await dataQueryService.GetData(StartTime, EndTime.AddDays(1), ProductModel, TaryNumber, EmployeeID, $"ProductionData_{StartTime:yyyy}{GetQuarterly(StartTime.Month)}");
            foreach (var item in list)
            {
                WeightList.Add(item);
            }
            DialogHost.CloseDialogCommand.Execute(new object(), sender);
        });

        private int GetQuarterly(int count)
        {
            switch (count)
            {
                case int p when p >= 1 && p < 4:
                    return 1;
                case int p when p >= 4 && p < 7:
                    return 2;
                case int p when p >= 7 && p < 10:
                    return 3;
                case int p when p >= 10 && p <= 12:
                    return 4;
            }
            return 1;
        }

        /// <summary>
        /// 导出数据
        /// </summary>
        public DelegateCommand<Button> ExportData => new DelegateCommand<Button>(async sender =>
        {
            Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
            openFileDialog.Title = "选择文件";
            openFileDialog.Filter = "所有文件(*.*)|*.*";
            openFileDialog.FileName = "选择文件夹";
            openFileDialog.FilterIndex = 1;
            openFileDialog.ValidateNames = false;
            openFileDialog.CheckFileExists = false;
            openFileDialog.CheckPathExists = true;
            openFileDialog.Multiselect = false;//允许同时选择多个文件
            if (openFileDialog.ShowDialog() == true)
            {
                _ = DialogHost.Show(new LoadingDialogs(), "RootDialog").ConfigureAwait(false);
                try
                {
                    await dataQueryService.ExportData(openFileDialog.FileName.Replace("选择文件夹", null, StringComparison.Ordinal), WeightList);
                    DialogHost.CloseDialogCommand.Execute(new object(), sender);
                    $"查询数据导出成功".MessageDialogs(Enums.MessageLevelType.Info);
                }
                catch (Exception ex)
                {
                    DialogHost.CloseDialogCommand.Execute(new object(), sender);
                    $"数据导出异常：{ex.Message}".MessageDialogs(Enums.MessageLevelType.Error);
                }
            }
            DialogHost.CloseDialogCommand.Execute(new object(), sender);
        });

        /// <summary>
        /// 上传数据
        /// </summary>
        public DelegateCommand<Button> MESRetransmission => new DelegateCommand<Button>(async sender =>
        {

            _ = DialogHost.Show(new LoadingDialogs(), "RootDialog").ConfigureAwait(false);
            await Task.Run(() =>
            {
                foreach (var item in WeightList)
                {
                    //if (item.MESOnline)
                    //{
                    //    break;
                    //}
                    List<Models.Testdatap> testdata = null;
                    #region 
                    //if (_parameter.RunParameter.ProductType == "二次注液")
                    //{
                    //    #region 旧
                    //    testdata = new List<Models.Testdatap>() {
                    //        new Models.Testdatap(){
                    //        ItemCode="电解液批次",
                    //            Value = item.BatchElectrolyte
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode="电解液料号",
                    //            Value = item.ElectrolyteMaterialNumber
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode = "循环次数",
                    //            Value = item.Cycles.ToString()
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode = "返工记录",
                    //            Value = "0",
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode = "注液前真空度",
                    //            Value = item.VacuumBeforeFilling.ToString("F2"),
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode = "注液量",
                    //            Value = item.FluidRate.ToString("F2"),
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode = "电解液氢氟酸含量",
                    //            Value = item.HydrofluoricAcidContent.ToString("F2"),
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode = "电解液水含量",
                    //            Value = item.ElectrolyteWaterContent.ToString("F2"),
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode = "电芯注液前重量",
                    //            Value = item.Preweight.ToString("F2"),
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode = "电芯注液后重量",
                    //            Value = item.Afterweight.ToString("F2"),
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode = "注液正压",
                    //            Value = item.PositivePressure.ToString("F2"),
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode = "注液负压",
                    //            Value = item.NegativePressure.ToString("F2"),
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode = "注液杯",
                    //            Value = item.CupNumber.ToString(),
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode = "注液站号",
                    //            Value = item.StationNo.ToString()
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode = "化成前后电解液损失量",
                    //            Value = item.LossAfterFormation.ToString("F2"),
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode = "回氦前真空度",
                    //            Value = item.VacuumBeforeReturningHelium.ToString("F2"),
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode = "回氦后真空度",
                    //            Value = item.VacuumDegreeAfterReturningHelium.ToString("F2"),
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode = "保有量",
                    //            Value = item.Ownership.ToString("F2"),
                    //        },
                    //    };
                    //    #endregion


                    //}
                    //else
                    //{
                    //    #region 旧
                    //    testdata = new List<Models.Testdatap>() {
                    //        new Models.Testdatap(){
                    //            ItemCode="电解液批次",
                    //            Value = item.BatchElectrolyte
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode="电解液料号",
                    //            Value=item.ElectrolyteMaterialNumber
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode = "循环次数",
                    //            Value = item.Cycles.ToString()
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode = "注液前真空度",
                    //            Value = item.VacuumBeforeFilling.ToString("F2"),
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode = "注液量",
                    //            Value = item.FluidRate.ToString("F2"),
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode = "电解液氢氟酸含量",
                    //            Value = item.HydrofluoricAcidContent.ToString("F2"),
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode = "电解液水含量",
                    //            Value = item.ElectrolyteWaterContent.ToString("F2"),
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode = "电芯注液前重量",
                    //            Value = item.Preweight.ToString("F2"),
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode = "电芯注液后重量",
                    //            Value = item.Afterweight.ToString("F2"),
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode = "注液正压",
                    //            Value = item.PositivePressure.ToString("F2"),
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode = "注液负压",
                    //            Value = item.NegativePressure.ToString("F2"),
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode = "注液杯",
                    //            Value = item.CupNumber.ToString(),
                    //        },
                    //        new Models.Testdatap(){
                    //            ItemCode = "注液站号",
                    //            Value = item.StationNo.ToString()
                    //        },
                    //    };
                    //    #endregion
                    //}
                    #endregion
                    #region 最新
                    if (_parameter.RunParameter.ProductType == "二次注液")
                    {
                        testdata = new List<Models.Testdatap>
                        {
                             new Models.Testdatap()
                            {
                                ItemCode = "工序名",
                                Value = $"{item.GroupCode}",
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "班次",
                                Value = $"{GetTimeSpan(DateTime.Now)}",
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "制令单",
                                Value = $"{item.MoNumber}",
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "组别",
                                Value =$"{item.GroupName}",
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "电芯条码",
                                Value = item.BarCode
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "设备编号",
                                Value =$"{_parameter.RunParameter.EquipNum}",
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "注液工位",
                                Value = item.StationNo.ToString()
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "进站时间",
                                Value = $"{item.FrontScanTime:yyyy-MM-dd HH:mm:ss}",
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "出站时间",
                                Value = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}",
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "电解液料号",
                                Value = item.ElectrolyteMaterialNumber
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "电解液批次号",
                                Value = item.BatchElectrolyte
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "电解液氢氟酸含量",
                                Value = item.HydrofluoricAcidContent.ToString("F2"),
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "电解液含水量",
                                Value = item.ElectrolyteWaterContent.ToString("F2"),
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "二注电芯注液前重量上限值（g）",
                                Value =(item.ForeweighRange.Split('-')[1]).ToString()
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "二注电芯注液前重量下限值（g）",
                                  Value =(item.ForeweighRange.Split('-')[0]).ToString()
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "二注电芯注液前重量实际值（g）",
                                Value = item.Preweight.ToString()
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "二注电芯注液后重量上限值（g）",
                                Value =(item.AfterWeighingRange.Split('-')[1]).ToString()
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "二注电芯注液后重量下限值（g）",
                               Value =(item.AfterWeighingRange.Split('-')[0]).ToString()
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "二注电芯注液后重量实际值（g）",
                                Value = item.Afterweight.ToString()
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "二注注液量上限值（g）",
                                Value =(item.FluidRateRange.Split('-')[1]).ToString()
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "二注注液量下限值（g）",
                                Value = (item.FluidRateRange.Split('-')[0]).ToString()
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "二注注液量实际值（g）",
                                Value = item.FluidRate.ToString()
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "液矢量实际值（g）",
                              Value = item.LossAfterFormation.ToString("F2"),
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "保液量设定值（g）",
                                Value =_parameter.WeighingParameter.WeighAfterFilling.ToString()
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "保液量上限值（g）",
                                Value =(_parameter.WeighingParameter.WeighAfterFilling + _parameter.WeighingParameter.FluctuationWeighAfterFilling).ToString()
                            },
                         new Models.Testdatap()
                        {
                            ItemCode = "保液量下限值（g）",
                            Value =(_parameter.WeighingParameter.WeighAfterFilling - _parameter.WeighingParameter.FluctuationWeighAfterFilling).ToString()
                        },
                          new Models.Testdatap()
                        {
                            ItemCode = "保液量实际值（g）",
                            Value = item.Ownership.ToString("F2"),
                        },
                           new Models.Testdatap()
                        {
                            ItemCode = "二注注液前真空度设定值（kPa）",
                            Value = item.VacuumBeforeFillingSetting.ToString("F2"),
                        },
                        new Models.Testdatap()
                        {
                            ItemCode = "二注注液前真空度实际值（kPa）",
                            Value = item.VacuumBeforeFilling.ToString("F2"),
                        },
                        new Models.Testdatap()
                        {
                            ItemCode = "二注注液正压设定值（kPa）",
                            Value = item.PositivePressureSetting.ToString("F2"),
                        },
                        new Models.Testdatap()
                        {
                            ItemCode = "二注注液正压实际值（kPa）",
                              Value = item.PositivePressure.ToString("F2"),
                        },
                        new Models.Testdatap()
                        {
                            ItemCode = "二注正压时间设定值（S）",
                            Value = item.TimePositivePressureSetting.ToString(),
                        },
                        new Models.Testdatap()
                        {
                            ItemCode = "二注正压时间实际值（S）",
                            Value = item.TimePositivePressure.ToString(),
                        },
                        new Models.Testdatap()
                        {
                            ItemCode = "二注注液负压设定值（kPa）",
                              Value = item.NegativePressureSetting.ToString("F2"),
                        },
                        new Models.Testdatap()
                        {
                            ItemCode = "二注注液负压实际值（kPa）",
                              Value = item.NegativePressure.ToString("F2"),
                        },
                        new Models.Testdatap()
                        {
                            ItemCode = "二注负压时间设定值（S）",
                            Value = item.TimeNegativePressureSetting.ToString(),
                        },
                        new Models.Testdatap()
                        {
                            ItemCode = "二注负压时间实际值（S）",
                            Value = item.TimeNegativePressure.ToString(),
                        },
                        new Models.Testdatap()
                        {
                            ItemCode = "二注循环次数",
                            Value = item.Cycles.ToString()
                        },
                        new Models.Testdatap()
                        {
                            ItemCode = "作业员",
                            Value = item.UserName
                        },
                        new Models.Testdatap()
                        {
                            ItemCode = "NG详情",
                             Value = $"{item.JudgementResultAfterWeighing}",
                        }
                        };
                    }
                    else
                    {
                        testdata = new List<Models.Testdatap>
                        {
                            new Models.Testdatap()
                            {
                                ItemCode = "工序名",
                                Value = $"{_parameter.RunParameter.GroupCode}",
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "班次",
                                Value = $"{GetTimeSpan(DateTime.Now)}",
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "制令单",
                                Value = $"{_parameter.RunParameter.MoNumber}",
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "组别",
                                Value =$"{_parameter.RunParameter.GroupName}",
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "电芯条码",
                                Value = item.BarCode
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "设备编号",
                                Value =$"{_parameter.RunParameter.EquipNum}",
                            },
                                new Models.Testdatap()
                            {
                                ItemCode = "注液工位",
                                Value = item.StationNo.ToString()
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "进站时间",
                                Value = $"{item.FrontScanTime:yyyy-MM-dd HH:mm:ss}",
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "出站时间",
                                Value = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss}",
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "电解液料号",
                                Value = item.ElectrolyteMaterialNumber
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "电解液批次号",
                                Value = item.BatchElectrolyte
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "电解液氢氟酸含量",
                                Value = item.HydrofluoricAcidContent.ToString("F2"),
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "电解液含水量",
                                Value = item.ElectrolyteWaterContent.ToString("F2"),
                            },
                               new Models.Testdatap()
                            {
                                ItemCode = "一注电芯注液前重量上限值（g）",
                              Value =(item.ForeweighRange.Split('-')[1]).ToString()
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "一注电芯注液前重量下限值（g）",
                                Value =(item.ForeweighRange.Split('-')[0]).ToString()
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "一注电芯注液前重量实际值（g）",
                                Value = item.Preweight.ToString()
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "一注电芯注液后重量上限值（g）",
                                Value =(item.AfterWeighingRange.Split('-')[1]).ToString()
                           },
                            new Models.Testdatap()
                            {
                                ItemCode = "一注电芯注液后重量下限值（g）",
                                 Value =(item.AfterWeighingRange.Split('-')[0]).ToString()
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "一注电芯注液后重量实际值（g）",
                                Value = item.Afterweight.ToString()
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "一注注液量上限值（g）",
                                 Value = (item.FluidRateRange.Split('-')[1]).ToString()
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "一注注液量下限值（g）",
                                Value = (item.FluidRateRange.Split('-')[0]).ToString()
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "一注注液量实际值（g）",
                                Value = item.FluidRate.ToString()
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "一注注液前真空度设定值（kPa）",
                                Value = item.VacuumBeforeFillingSetting.ToString("F2"),
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "一注注液前真空度实际值（kPa）",
                                Value = item.VacuumBeforeFilling.ToString("F2"),
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "一注注液正压设定值（kPa）",
                                Value =  item.PositivePressureSetting.ToString("F2"),
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "一注注液正压实际值（kPa）",
                                Value = item.PositivePressure.ToString("F2"),
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "一注单次正压时间设定值",
                                 Value = item.TimePositivePressureSetting.ToString(),
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "一注单次正压时间实际值",
                                 Value = item.TimePositivePressure.ToString(),
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "一注注液负压设定值（kPa）",
                                Value = item.NegativePressureSetting.ToString("F2"),
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "一注注液负压实际值（kPa）",
                                Value = item.NegativePressure.ToString("F2"),
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "一注单次负压时间设定值（S）",
                                Value = item.TimeNegativePressureSetting.ToString(),
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "一注单次负压时间实际值（S）",
                               Value = item.TimeNegativePressure.ToString(),
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "一注循环次数",
                                Value = item.Cycles.ToString()
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "作业员",
                                Value = item.UserName
                            },
                            new Models.Testdatap()
                            {
                                ItemCode = "NG详情",
                                 Value = $"{item.JudgementResultAfterWeighing}",
                            }
                        };

                    }
                    #endregion 

                    #region 参数
                    testdata.Add(new Models.Testdatap()
                    {
                        ItemCode = "作业员",
                        Value = $"{item.UserName}",
                    });
                    testdata.Add(new Models.Testdatap()
                    {
                        ItemCode = "设备机台号",
                        Value = $"{item.EquipmentIdentity}",
                    });
                    testdata.Add(new Models.Testdatap()
                    {
                        ItemCode = "投入时间",
                        Value = $"{item.FrontScanTime:yyyy-MM-dd HH:mm:ss}",
                    });
                    testdata.Add(new Models.Testdatap()
                    {
                        ItemCode = "产出时间",
                        Value = $"{item.BackWeighTime:yyyy-MM-dd HH:mm:ss}",
                    });
                    testdata.Add(new Models.Testdatap()
                    {
                        ItemCode = "电芯条码",
                        Value = $"{item.BarCode}",
                    });
                    testdata.Add(new Models.Testdatap()
                    {
                        ItemCode = "工序名",
                        Value = $"{item.GroupCode}",
                    });
                    testdata.Add(new Models.Testdatap()
                    {
                        ItemCode = "班次",
                        Value = $"{GetTimeSpan(DateTime.Now)}",
                    });
                    testdata.Add(new Models.Testdatap()
                    {
                        ItemCode = "综合判定结果",
                        Value = $"{item.JudgementResultAfterWeighing == NGType.合格}",
                    });
                    testdata.Add(new Models.Testdatap()
                    {
                        ItemCode = "NG详情",
                        Value = $"{item.JudgementResultAfterWeighing}",
                    });
                    testdata.Add(new Models.Testdatap()
                    {
                        ItemCode = "package号",
                        Value = $"{item.MoNumber}",
                    });
                    #endregion
                    MyMesHelper.TestDataGroupInfo(new Models.ProcessParametersModel()
                    {
                        GroupCode = item.GroupCode,
                        GroupName = item.GroupName,
                        OperatorID = item.UserName,
                        ProductSn = item.BarCode,
                        TimeStamp = item.FrontScanTime.ToString("yyyy-MM-dd HH:mm:ss"),
                        MachineID = item.EquipmentIdentity,
                        ProductModel = item.MesUpdateMode ? "Online" : "Offline",
                        MoNumber = item.MoNumber,
                        TestResult = item.JudgementResultAfterWeighing == Enums.NGType.合格 ? "0" : "1",
                        Material = new Models.Materialp[] {
                                    new Models.Materialp(){
                                        MaterialLot = item.BatchElectrolyte,
                                        MaterialNo = item.ElectrolyteMaterialNumber,
                                    }
                                },
                        TestData = testdata.ToArray(),
                    }, _parameter.MESParameter.TestDataGroupInfoNumber);
                    item.MESOnline = true;
                    item.Update();
                }
            });
            DialogHost.CloseDialogCommand.Execute(new object(), sender);
        });

        protected string GetTimeSpan(DateTime timeStr)
        {
            //判断当前时间是否在工作时间段内
            string _strWorkingDayAM = "08:30";//工作时间上午08:30
            string _strWorkingDayPM = "18:00";
            TimeSpan dspWorkingDayAM = DateTime.Parse(_strWorkingDayAM).TimeOfDay;
            TimeSpan dspWorkingDayPM = DateTime.Parse(_strWorkingDayPM).TimeOfDay;
            //string time1 = "2017-2-17 8:10:00";
            //DateTime t1 = Convert.ToDateTime(timeStr);
            TimeSpan dspNow = timeStr.TimeOfDay;
            if (dspNow > dspWorkingDayAM && dspNow < dspWorkingDayPM)
            {
                return "早班";
            }
            return "晚班";
        }
    }
}
